package com.shimmer;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.shimmer.view.AboutFragment;
import com.shimmer.view.HomeFragment;
import com.shimmer.view.StatusFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    int page;
    private LinearLayout mOneLin, mTwoLin, mThreeLin;
    private HomeFragment mOneFragment;
    private StatusFragment mTwoFragment;
    private AboutFragment mThreeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setSwPage(0);
    }

    private void initView() {
        mOneLin = findViewById(R.id.one_lin);
        mTwoLin = findViewById(R.id.two_lin);
        mThreeLin = findViewById(R.id.three_lin);
        mOneLin.setOnClickListener(this);
        mTwoLin.setOnClickListener(this);
        mThreeLin.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one_lin:
                setSwPage(0);
                break;
            case R.id.two_lin:
                setSwPage(1);
                break;
            case R.id.three_lin:
                setSwPage(2);
                break;
        }
    }

    //页面跳转
    public void setSwPage(int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            case 0:
                page = 1;
                reLinSelect();
                mOneLin.setSelected(true);
                if (mOneFragment == null) {
                    mOneFragment = new HomeFragment();
                    transaction.add(R.id.fl_container, mOneFragment);
                } else {
                    //如果对应的Fragment已经实例化，则直接显示出来
                    transaction.show(mOneFragment);
                }
                break;
            case 1:
                page = 2;
                reLinSelect();
                mTwoLin.setSelected(true);
                if (mTwoFragment == null) {
                    mTwoFragment = new StatusFragment();
                    transaction.add(R.id.fl_container, mTwoFragment);
                } else {
                    transaction.show(mTwoFragment);
                }
                break;
            case 2:
                page = 3;
                reLinSelect();
                mThreeLin.setSelected(true);
                if (mThreeFragment == null) {
                    mThreeFragment = new AboutFragment();
                    transaction.add(R.id.fl_container, mThreeFragment);
                } else {
                    transaction.show(mThreeFragment);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (mOneFragment != null) {
            transaction.hide(mOneFragment);
        }
        if (mTwoFragment != null) {
            transaction.hide(mTwoFragment);
        }
        if (mThreeFragment != null) {
            transaction.hide(mThreeFragment);
        }
    }

    //初始化底部菜单选择状态
    private void reLinSelect() {
        mOneLin.setSelected(false);
        mTwoLin.setSelected(false);
        mThreeLin.setSelected(false);
    }



}
